<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学号" prop="studentNumber">
        <el-input
          v-model="queryParams.studentNumber"
          placeholder="请输入学号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专业" prop="major">
        <el-input
          v-model="queryParams.major"
          placeholder="请输入专业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <div>
      <el-card>
        <el-table :data="tableData">
          <el-table-column prop="stuId" label="学号" />
          <el-table-column prop="name" label="姓名" />
          <el-table-column prop="gender" label="性别" />
          <el-table-column prop="provinceOfOrigin" label="籍贯" />
          <el-table-column prop="department" label="院系" />
        </el-table>
      </el-card>

      <el-card>
        <h3>性别统计 - 柱状图</h3>
        <ECharts :options="genderChartOptions" />
      </el-card>

      <el-card>
        <h3>籍贯分布 - 折线图</h3>
        <ECharts :options="provinceChartOptions" />
      </el-card>

      <el-card>
        <h3>院系分布 - 饼图</h3>
        <ECharts :options="departmentChartOptions" />
      </el-card>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />



  </div>
</template>

<script>
import { listStuInfo, getStuInfo, delStuInfo, addStuInfo, updateStuInfo ,getReportManager } from "@/api/stuSys/stuInfo";
import axios from 'axios';
import ECharts from "@/components/ECharts/ECharts.vue";

export default {
  components: { ECharts },
  name: "StuInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生学籍信息管理表格数据
      stuInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentNumber: null,
        name: null,
        provinceOfOrigin: null,
        major: null,
        department: null,
      },
      provinceOfOrigins: [
        { value: '山西省', label: '山西省' },
        { value: '北京市', label: '北京市' },
        { value: '天津市', label: '天津市' },
      ],
      departments: [
        { value: '计算机学院', label: '计算机学院' },
        { value: '数学学院', label: '数学学院' },
        { value: '电子信息学院', label: '电子信息学院' },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        studentNumber: [
          { required: true, message: "学号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        gender: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        provinceOfOrigin: [
          { required: true, message: "生源省份不能为空", trigger: "change" }
        ],
        nativePlace: [
          { required: true, message: "籍贯不能为空", trigger: "blur" }
        ],
        dateOfBirth: [
          { required: true, message: "出生日期不能为空", trigger: "blur" }
        ],
        major: [
          { required: true, message: "专业不能为空", trigger: "blur" }
        ],
        department: [
          { required: true, message: "院系不能为空", trigger: "change" }
        ],
        entranceScore: [
          { required: true, message: "入学分数不能为空", trigger: "blur" }
        ],
        phoneNumber: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
      },
      chartOptions: {
        title: {
          text: '用户统计'
        },
        tooltip: {},
        xAxis: {
          type: 'category',
          data: []
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '用户数量',
            type: 'line',
            data: []
          }
        ]
      },
      tableData: [],

      genderChartOptions: {
        title: {
          text: '性别统计'
        },
        tooltip: {},
        xAxis: {
          type: 'category',
          data: ['男', '女']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '人数',
            type: 'bar',
            data: []
          }
        ]
      },

      provinceChartOptions: {
        title: {
          text: '籍贯分布'
        },
        tooltip: {},
        xAxis: {
          type: 'category',
          data: []
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '人数',
            type: 'line',
            data: []
          }
        ]
      },

      departmentChartOptions: {
        title: {
          text: '院系分布'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          left: 'center'
        },
        series: [
          {
            name: '人数',
            type: 'pie',
            radius: '50%',
            data: [],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    /** 查询学生学籍信息管理列表 */
    getList() {
      this.loading = true;
      listStuInfo(this.queryParams).then(response => {
        this.stuInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentNumber: null,
        name: null,
        gender: null,
        provinceOfOrigin: null,
        nativePlace: null,
        dateOfBirth: null,
        major: null,
        department: null,
        entranceScore: null,
        photo: null,
        phoneNumber: null,
        personalIntroduction: null,
        backup1: null,
        backup2: null,
        backup3: null,
        backup4: null,
        backup5: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生学籍信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStuInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生学籍信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStuInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStuInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学生学籍信息管理编号为"' + ids + '"的数据项？').then(function() {
        return delStuInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('stuSys/stuInfo/export', {
        ...this.queryParams
      }, `stuInfo_${new Date().getTime()}.xlsx`)
    },
    /** Word导出按钮操作 */
    handleExport2(row) {
      const ids = row.id || this.ids;
      getReportManager(ids).then(response => {
        const url = window.URL.createObjectURL(new Blob([response]));
        const link = document.createElement('a');
        link.style.display = 'none';
        link.href = url;
        link.setAttribute('download', '报告.zip');
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      })
    },
    fetchData() {
      listStuInfo({}).then(response => {
        const data = response.rows || []; // 使用 response.rows，确保不为空数组

        // 设置表格数据
        this.tableData = data;

        // 处理性别数据，统计男女性别人数
        const genderCount = { 男: 0, 女: 0 };
        const provinceCount = {};
        const departmentCount = {};

        data.forEach(item => {
          // 统计性别人数
          if (item.gender === '男') {
            genderCount.男 += 1;
          } else if (item.gender === '女') {
            genderCount.女 += 1;
          }

          // 统计籍贯分布
          if (!provinceCount[item.provinceOfOrigin]) {
            provinceCount[item.provinceOfOrigin] = 1;
          } else {
            provinceCount[item.provinceOfOrigin] += 1;
          }

          // 统计院系分布
          if (!departmentCount[item.department]) {
            departmentCount[item.department] = { value: 1, name: item.department };
          } else {
            departmentCount[item.department].value += 1;
          }
        });

        // 设置性别柱状图数据
        this.genderChartOptions.series[0].data = [genderCount.男, genderCount.女];

        // 设置籍贯折线图数据
        this.provinceChartOptions.xAxis.data = Object.keys(provinceCount);
        this.provinceChartOptions.series[0].data = Object.values(provinceCount);

        // 设置院系饼图数据
        this.departmentChartOptions.series[0].data = Object.values(departmentCount);
      }).catch(error => {
        console.error('数据加载失败', error);
      });
    }
  }
};
</script>
<style scoped>
</style>
